{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2e0ad694",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.b90ac3a95863483806c8fcbbd85c4f49.315360000.1965091686.282335-25863427', 'expires_in': 2592000, 'session_key': '9mzdXvya8mlZ9X11/cwt60mDPVpZIE+ZjykACzJy+yz6MfmQCYtxOmelxRcJWw3qtyU3fRoPl4o3FZ5sBG3xxYrC08Z7pw==', 'access_token': '24.e973dfbc3e2dc2438d63114a38f7d2a4.2592000.1652323686.282335-25863427', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '24838b8e0f42bde9ca1228eef424a3e9'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'MaLl2hBQpo4t2RnIbIUWTyVc',\n",
    "    'client_secret':'85yagx5kWrrzU1BRbl1m9RmflRsWb9i1'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "13425377",
   "metadata": {},
   "outputs": [],
   "source": [
    "wujia_AT = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "21addc88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.e973dfbc3e2dc2438d63114a38f7d2a4.2592000.1652323686.282335-25863427'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wujia_AT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a76cf05f",
   "metadata": {},
   "source": [
    "#### 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a75b280a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f6a18a09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '卡通动漫人物', 'score': 0.892052, 'root': '非自然图像-彩色动漫'}, {'keyword': '肯德基', 'score': 0.799948, 'root': 'Logo'}, {'keyword': '屏幕截图', 'score': 0.466237, 'root': '非自然图像-屏幕截图'}, {'keyword': '图标', 'score': 0.24411, 'root': '非自然图像-图像素材'}, {'keyword': '简笔画', 'score': 0.041855, 'root': '非自然图像-简笔画'}], 'log_id': 1513710565943760083}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('kfc.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = wujia_AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "63d98371",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '葡萄酒', 'score': 0.966426, 'root': '商品-瓶饮'}, {'keyword': '洋酒', 'score': 0.648711, 'root': '商品-瓶饮'}, {'keyword': '红酒', 'score': 0.400873, 'root': '商品-瓶饮'}, {'keyword': '酒', 'score': 0.199643, 'root': '植物-果实/种子'}, {'keyword': '白酒', 'score': 0.00504, 'root': '商品-瓶饮'}], 'log_id': 1513710567972527082}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('hongjiu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = wujia_AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ece4b5d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'keyword': '螃蟹', 'score': 0.173636, 'root': '商品-食物'}, {'keyword': '食物', 'score': 0.159502, 'root': '动物-节肢动物'}, {'keyword': '菜肴', 'score': 0.116537, 'root': '商品-其他'}, {'keyword': '花卉', 'score': 0.057217, 'root': '商品-农用物资'}, {'keyword': '湖蟹', 'score': 0.00385, 'root': '动物-甲壳类'}], 'log_id': 1513721619477523489}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('meishi.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = wujia_AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4300212b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
